<?php

/**
* alltosun.com 收货地址控制器 index.php
* ============================================================================
* 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
* 网站地址: http://www.alltosun.com
* ----------------------------------------------------------------------------
* 许可声明: 这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
* ============================================================================
* $Author: 吴松键 (wusj@alltosun.com) $
* $Date: 2014-9-4 上午9:22:02 $
* $Id$
*/

class Action
{
    private $user_id;

    public function __construct()
    {
        checkLogin(get_url());
        $this->user_id = user_helper::get_user_id();
    }

    public function __call($action = "", $param = array()){
        $type = Request::getParam('type', '');

        $address_list = _widget('address')->get_list();
        if (!$address_list && $type == 'service') {
            Response::redirect(AnUrl('address/center_add?type=service'));
            Response::flush();
            exit;
        }
        Response::assign('type', $type);
        Response::assign('address_list', $address_list);

        display_template('address.html');
    }

    /**
     * 历史遗留问题 下单选择收获地址
     */
    public function slt_address()
    {
        $use_id = Request::getParam('use_id', 0);

        $direct_sku_id  = Request::Get('sku_id', 0);
        $direct_buy_num = Request::Get('num', 0);

        $address_list = _widget('address')->get_list();
        if (count($address_list) == 0) {
            Response::redirect(AnUrl("address/add?type=trade&sku_id={$direct_sku_id}&num={$direct_buy_num}"));
            Response::flush();
            exit;
        }

        $_SESSION['direct_sku_id'] = $direct_sku_id;
        $_SESSION['direct_buy_num'] = $direct_buy_num;

        Response::assign('use_id', $use_id);
        Response::assign('address_list', $address_list);

        Response::assign('direct_sku_id', $direct_sku_id);
        Response::assign('direct_buy_num', $direct_buy_num);

        Response::display('m/slt_list.html');
    }

    /**
     * 历史遗留问题 add
     */
    public function add()
    {
        $id = Request::getParam('id', 0);
        $type = Request::getParam('type', '');

        if ($id) {
            $address_info = _uri('address', $id);
            if ($address_info['user_id'] != $this->user_id) {
                return array('请选择自己的收获地址', 'error', AnUrl('address/slt_add'));
            }

            Response::assign('address_info', $address_info);
        }

        Response::assign('type', $type);
        Response::display('m/add.html');
    }

    /**
     * 个人中心添加地址
     */
    public function center_add()
    {
        $id = Request::getParam('id', 0);
        $type = Request::getParam('type', '');

        if ($id) {
            $address_info = _uri('address', $id);
            if ($address_info['user_id'] != $this->user_id) {
                return array('请选择自己的收获地址', 'error', AnUrl('address/slt_add'));
            }

            Response::assign('address_info', $address_info);
        }

        Response::assign('type', $type);
        Response::display('m/center_add.html');
    }

    /**
     * ajax 获取地址信息
     */
    public function get_address()
    {
        $id      = Request::getParam('id', 0);

        if (!$id) {
          return '请选择需要修改的地址！';
        }

        $address_info = _uri('address', $id);
        if ($address_info['user_id'] != $this->user_id) {
            return "请选择自己的收获地址";
        }

        if (!empty($address_info)) {
            return array('info'=>'ok', 'address'=>$address_info);
        }

        return 'ok';
    }

    /**
     * ajax 保存地址
     */
    public function save_address()
    {
        $id      = Request::getParam('id', 0);
        $info['tel']        = Request::getParam('telephone', '');
        $info['receiver']   = Request::getParam('receiver', '');
        $province           = Request::getParam('province', '');
        $city               = Request::getParam('city', '');
        $district           = Request::getParam('district', '');
        $info['address']    = Request::getParam('address', '');
        $info['mobile']     = Request::getParam('mobile', '');
        $info['is_default'] = Request::getParam('is_default', 0);
        $info['user_id']    = $this->user_id;
        $return_order       = Request::getParam('return_order', 0);

        // 判断列表
        if (!$info['receiver']) {
            return '请填写用户名';
        }
        if (!$province) {
            return '请选择省份';
        }
        if (!$city) {
            return '请选择城市';
        }
        if (!$district) {
            return '请选择区';
        }
        if (!$info['address']) {
            return '请填写详细地址';
        }
        if ($info['mobile'] && !preg_match('/^1[345789][0-9]{9}/', $info['mobile'])) {
            return '请填写正确的手机号码';
        }
        if (!($info['mobile'] || $info['tel'])) {
            return '手机号或固话最少填写一个';
        }

        foreach ($info as $k=>$v) {
            if (!is_numeric($v)) {
                $info[$k] = AnFilter::filter_string(trim($v));
            }
        }
        $info['province']    = _uri('provincial', $province, 'title');
        $info['city']        = _uri('provincial', $city, 'title');
        $info['district']    = _uri('provincial', $district, 'title');
        $info['province_id'] = $province;
        $info['city_id']     = $city;
        $info['district_id'] = $district;
        if ($id) {
            $address_info = _uri('address', $id);
            if ($address_info['user_id'] != $this->user_id) {
                return "请选择自己的收获地址";
            }

            if ($info['is_default'] != 1) {
                $info['is_default'] = _uri('address', $id, 'is_default');
            }
            _model('address')->update($id, $info);
        } else {
            $address_total = _model('address')->getTotal(array('user_id'=>$this->user_id));
            if ($address_total < 1) {
                $info['is_default'] = 1;
            }
            $id = _model('address')->create($info);
        }

        // 将其他地址设为非默认
        if ($info['is_default']) {
            _model('address')->update(array('user_id'=>$this->user_id, '`id` <> '=>$id), array('is_default'=>0));
        }

        if (Request::isAjax()) {
            return array(
                            'info'=>'ok',
                            'id'=>$id,
                            'is_default'=>$info['is_default'],
                            'sku_id' => !empty($_SESSION['direct_sku_id']) ? $_SESSION['direct_sku_id'] : '',
                            'num'    => !empty($_SESSION['direct_buy_num']) ? $_SESSION['direct_buy_num'] : '',
            );
        } elseif ($return_order == 1) {
            if(!empty($_SESSION['direct_sku_id'])) {
                Response::redirect(AnUrl("order/trade?sku_id={$_SESSION['direct_sku_id']}&num={$_SESSION['direct_buy_num']}"));
            } else {
                Response::redirect(AnUrl('order/trade'));
            }

        } else {
            Response::redirect(AnUrl('address'));
        }
    }

    /**
     * ajax 设置默认地址
     */
    public function set_default_address()
    {
        $id = Request::getParam('id', 0);

        if (!$id) {
            return '请选择需要修改的地址！';
        }
        $address_info = _uri('address', $id);
        if ($address_info['user_id'] != $this->user_id) {
            return "请选择自己的收获地址";
        }

        _model('address')->update(array('user_id'=>$this->user_id), array('is_default'=>0));
        _model('address')->update($id, array('is_default'=>1));

        return 'ok';
    }

    /**
     * ajax 删除地址
     */
    public function delete_address()
    {
        $id = Request::getParam('id', 0);

        if (!$id) {
            return '请选择需要删除的地址！';
        }
        $address_info = _uri('address', $id);
        if ($address_info['user_id'] != $this->user_id) {
            return "请选择自己的收获地址";
        }

        $is_default = _uri('address', $id, 'is_default');

        _model('address')->delete($id);
        $address_id = 0;
        if ($is_default) {
            $address_list = _model('address')->getList(array('user_id'=>$this->user_id));
            if (count($address_list) > 0) {
                $address_id = $address_list[0]['id'];
                _model('address')->update($address_id, array('is_default'=>1));
            }
        }

        return array('info'=>'ok', 'new_default'=>$address_id);
    }
}
?>